libname ra "E:\data";/*cohort*/
libname pd "Z:\Medicare_20\Part_D";
libname pb "Z:\Medicare_20\Part_b";
libname mbsf "Z:\Medicare_20\mbsf";/*input dataset*/

****PURPOSE : GENERATE STATS FOR TABLES
                Note : ra.stack_1a is a dataset with desc and LIS variables***********;



******************************************************************
*********************TABLE 1***************************************;
********************************************************************;
proc means data= ra.stack_1a n nmiss min p25 median p75 max mean std;
var  age_index2;
where n_rx=1;
run;

proc freq data= ra.stack_1a;
tables bene_sex_ident_cd;
where n_rx=1;
run;

proc freq data= ra.stack_1a;
tables race;
where n_rx=1;
run;

proc freq data= ra.stack_1a;
tables lis_pre_bin;
where n_rx=1;
run;

proc freq data= ra.stack_1a;
tables CS_1yr_pr_index2  MTX_1yr_index2 GC_1yr_pr_index2/missing;
where n_rx=1;
run;
proc freq data= ra.stack_1a;
tables gc_long_1yr/missing;
where n_rx=1;
run;

proc freq data= ra.stack_1a;
tables rh_visit_1yr;
where n_rx=1;
run;

proc freq data= ra.stack_1a;;
tables region;
where n_rx=1 ;
run;


***dataset for comorb***;
proc sort data= ra.ra_comorb1a; by bene_id; run;
data ra_comorb1a;
set ra.ra_comorb1a;
by bene_id;
if first.bene_id;
run;

proc means data= ra_comorb1a n nmiss min p25 p50 p75 max mean std;
var  EH_BL1;
run;



*********************************************************************
**************************TABLE by de-eacaltion yes / no***********************************;

*****code desc var needed for tables and analysis*****;
data ra.stack_1a;
set ra.stack_1a;

***********de-escalation as bin var*****;
 if desc= "None" then desc01=0;
 else desc01= 1;

 ***************3 level***************;
 if desc= "Dose Reduction and Interval increase" or desc= "Dose Reduction" then desc1= "Decrease";
 else desc1 = desc;

 run;

****************************2 level desc**************************;
proc freq data= ra.stack_1a;
tables desc desc1 desc01;
where n_rx=1;
run;

proc means data= ra.stack_1a n min p25 median p75 max mean std;
class desc01;
var  age_index2;
where n_rx=1;
run;

proc npar1way data= ra.stack_1a wilcoxon;
class desc01;
var  age_index2;
where n_rx=1;
run;

proc freq data= ra.stack_1a;
tables bene_sex_ident_cd *desc01/chisq;
where n_rx=1;
run;

proc freq data= ra.stack_1a;
tables race *desc01/chisq;
where n_rx=1;
run;

proc freq data= ra.stack_1a;
tables lis_pre_bin *desc01/chisq;
where n_rx=1;
run;

proc freq data= ra.stack_1a;
tables rh_visit_1yr *desc01/chisq;
where n_rx=1;
run;

****code vars with . as 0******;
data stack_1a;
set ra.stack_1a;
if GC_1yr_pr_index2=. then GC_1yr_pr_index2a=0;
else GC_1yr_pr_index2a=GC_1yr_pr_index2 ;

if gc_long_1yr=. then gc_long_1yra= 0;
else gc_long_1yra= gc_long_1yr;

if MTX_1yr_index2= . then MTX_1yr_index2a= 0;
else MTX_1yr_index2a= MTX_1yr_index2;

if CS_1yr_pr_index2= . then CS_1yr_pr_index2a = 0;
else CS_1yr_pr_index2a = CS_1yr_pr_index2;

run;

***gc any**;


proc freq data=stack_1a;
tables GC_1yr_pr_index2a *desc01/ chisq ;
where n_rx=1;
run;

***gc long term***;
proc freq data=stack_1a;
tables gc_long_1yra *desc01/ chisq ;
where n_rx=1;
run;

***mtx****;

proc freq data= stack_1a;
tables MTX_1yr_index2a *desc01/chisq missing ;
where n_rx=1;
run;


*****cs***;

proc freq data= stack_1a;
tables CS_1yr_pr_index2a *desc01/chisq  missing ;
where n_rx=1;
run;

************get desc vars for comorb dataset****;

data stack_desc;
set stack_1a;
where n_rx=1;
keep bene_id desc desc1 desc01;
run;

proc sort data= stack_desc;by bene_id;run;
proc sort data= ra.ra_comorb1a out= ra_comorb1a; by bene_id;run;

data comorb_desc;
merge ra_comorb1a stack_desc;
by bene_id;
if first.bene_id;/*get baseline val*/
run;


proc means data= comorb_desc n nmiss min p25 p50 p75 max mean std;
class desc01;
var EH_BL1;
run;

proc npar1way data= comorb_desc wilcoxon;
class desc01;
var EH_BL1;
run;

*****************************************************************
******************get fu time************************************
*****************************************************************;
proc sort data=ra.stack_1a out=stack_1a; by bene_id;run;

data stop_dt;
set stack_1a;
by bene_id;
if last.bene_id;/*keep last obs*/
keep bene_id  stop_dt;
if desc ne "None" then stop_dt= desc_dt;
else if desc = "None" then stop_dt= rx_dt;/*last rx dt*/
if idswitch=1 then stop_dt= last_prim_index_dt;/*if switiched meds, then FU stops at time of switch*/
format stop_dt date9. ;
keep bene_id stop_dt rx_dt desc desc_dt desc01 desc1 last_prim_index_dt idswitch;
run;

proc sort data= stop_dt;by bene_id;run;
proc sort data= stack_1a; by bene_id;run;
data fu;
merge stack_1a stop_dt;
by bene_id;
fu= stop_dt- index2;
fu_yr= (stop_dt-index2)/365;
fu_mo= (stop_dt-index2)/30;
run;


proc means data= fu n mean std min p25 median p75 max;
var   fu_yr;
where n_rx=1;
run;

proc means data= fu n mean std min p25 median p75 max;
class desc01;
var   fu_yr;
where n_rx=1;
run;


